<?php
/*! \brief
 * This is Logger class main purpose for log message to /path/to/repository/log/diagnostic.log
 * Usage : 
 * 1)WF::getLogger()->fatal($msg);
 * 2)WF::getLogger()->error($msg);
 * 3)WF::getLogger()->warn($msg);
 * 4)WF::getLogger()->info($msg);
 * 5)WF::getLogger()->debug($msg);
 * \author William Su
 */
require_once dirname(__FILE__).'/../../../../lib/log4php/src/main/php/Logger.php';
require_once dirname(__FILE__).'/../../../../lib/log4php/src/main/php/LoggerRoot.php';
class WFLogger extends LoggerRoot {
    /**
     * @var WF
     */
    protected $objFramework;
    public function __construct($name,$objFramework){
        parent::__construct();
        $this->objFramework = $objFramework;
    }
	/**
	 *\brief Description
	 * - Get Error Message by $exception
	 */
	public function getMessageByException($exception){
	    $ret = $this->getMessage($exception->getMessage(), $exception->getFile(), $exception->getLine(), $exception->__toString());
	    return $ret;
	}
	/**
	 *\brief Description
	 * - Get Error Message by $parameter
	 */
	public function getMessage($strMsg,$strFile = null,$strLine = null,$strErrorStack = null){
	    $ret = $strMsg;
	    if(null !== $strFile && null !== $strLine){
	        $ret .='('.$strFile.' : '.$strLine.')';
	    }
	    if(null !== $strErrorStack){
	        $ret .= '['.$strErrorStack.']';
	    }
	    return $ret;
	}
	/**
	 *\brief Description
	 * - log message by loglevel
	 */
	public function logInfo($strMsg, $strLogLevel){
	    switch($strLogLevel){
    	    case 'DEBUG':
    	        return $this->debug($strMsg);
    	    break;
    	    case 'INFO':
    	        return $this->info($strMsg);
    	    break;
    	    case 'WARN':
    	        return $this->warn($strMsg);
    	    break;
    	    case 'ERROR':
    	        return $this->error($strMsg);
    	    break;
    	    case 'FATAL':
    	        return $this->fatal($strMsg);
    	    break;
    	    default:
    	        $strMsg = __CLASS__.'::'.__FUNCTION__.'()=>$strLogLevel:'.$strLogLevel.' incorrect!';
    	        throw new WFLogLoggerException($strMsg);
    	    break;
	    }
	}
	/**
	 *\brief Description
	 * - Get debug backtrace for diagnostic log.
	 * - Debug use
	 */
    public function getDebugBacktrace(){
        $bt = debug_backtrace();
        $output = '';
        $output .= "\n\nBacktrace (most recent call last):\n\n\n";    
        for($i = 0; $i <= count($bt) - 1; $i++)
        {
            if(!isset($bt[$i]["file"]))
                $output .= "[PHP core called function]\n";
            else
                $output .= "File: ".$bt[$i]["file"]."\n";
            
            if(isset($bt[$i]["line"]))
                $output .= "    line ".$bt[$i]["line"]."\n";
            $output .= "    function called: ".$bt[$i]["function"];
            
            if($bt[$i]["args"]){
                $output .= "\n    args: ";
                for($j = 0; $j <= count($bt[$i]["args"]) - 1; $j++){
                    if(is_array($bt[$i]["args"][$j])){
                        $output .= $this->objFramework->getVarLog($bt[$i]["args"][$j]);
                    }else{
                        $output .= $bt[$i]["args"][$j];    
                    }            
                    if($j != count($bt[$i]["args"]) - 1)
                        $output .= ", ";
                }
            }
            $output .= "\n\n";
        }
        $output = $this->objFramework->getVarLog($output);
        return $output;
    }
}